package com.gnerv.sylvanas.authentication.security.tool;

import com.gnerv.sylvanas.authentication.security.model.AccountDetails;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.session.SessionAuthenticationException;
import org.springframework.util.ObjectUtils;

/**
 * @Author Gnerv LiGen
 * @create 2023/6/13 10:54
 */
public class SecurityTools {

    private SecurityTools() {
    }

    public static String username() {
        return userDetails().getUsername();
    }

    public static AccountDetails userDetails() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (ObjectUtils.isEmpty(authentication)) {
            throw new SessionAuthenticationException("");
        }
        return (AccountDetails) authentication.getPrincipal();
    }

}
